package com.mobilesales.android.adapters;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.tasks.ListTask;
import com.mobilesales.android.infrastructure.CalendarDayModel;
import com.mobilesales.android.infrastructure.CalendarTile;
import com.mobilesales.android.util.Constants;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CalendarAdapter extends ArrayAdapter<CalendarDayModel> {

	private List<CalendarDayModel> days;

	public CalendarAdapter(Context context, int textViewResourceId,
			List<CalendarDayModel> objects) {
		super(context, textViewResourceId, objects);
		days = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.calendar_day, null);
		}

		CalendarDayModel day = null;
		if (position >= 0 && position < days.size()) {
			day = days.get(position);
		}

		if (day != null) {
			RelativeLayout container = (RelativeLayout) v
					.findViewById(R.id.dayContainer);
			TextView dayField = (TextView) v.findViewById(R.id.dayTxt);
			
			dayField.setText(day.getLabel());
			switch(day.getTileType()) {
				case CurrentDay:
					dayField.setTypeface(null, Typeface.BOLD);
					container.setBackgroundResource(R.drawable.day_current);
					break;
				case Holiday:
					container.setBackgroundResource(R.drawable.day_normal);
					break;
				case Invisible:
					container.setBackgroundResource(R.drawable.day_invisible);
					break;
				case Label:
					container.setBackgroundResource(R.drawable.day_invisible);
					break;
				case NormalDay:
					container.setBackgroundResource(R.drawable.day_normal);
					break;
				case TaskDay:
					dayField.setTypeface(null, Typeface.BOLD);
					dayField.setTextColor(Color.WHITE);
					container.setBackgroundResource(R.drawable.day_task);
					break;
			}

			final Date date = day.getDate();
			final Context ctx = this.getContext();
			if (date != null) {
				container.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(ctx, ListTask.class);
						intent.putExtra(Constants.SelectedDate, date);
						ctx.startActivity(intent);
					}
				});
			}
		}
		return v;
	}

	public static CalendarAdapter generateAdapter(Context ctx, int year,
			int month) {
		return generateAdapter(ctx, year, month, null);
	}

	public static CalendarAdapter generateAdapter(Context ctx, int year,
			int month, List<Integer> taskDates) {

		ArrayList<CalendarDayModel> days = new ArrayList<CalendarDayModel>();
		int[] dayNameResIds = new int[] { R.string.Mon, R.string.Tue,
				R.string.Wed, R.string.Thu, R.string.Fri, R.string.Sat,
				R.string.Sun };

		for (int i = 0; i < dayNameResIds.length; i++) {
			String dayName = ctx.getString(dayNameResIds[i]);
			CalendarDayModel day = new CalendarDayModel();
			day.setLabel(dayName);
			day.setTileType(CalendarTile.Label);
			days.add(day);
		}

		month = month - 1;
		GregorianCalendar calendar = new GregorianCalendar(year, month, 1);
		int daysInMonth = calendar
				.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
		int dayOfWeekFirst = 9 - calendar.get(GregorianCalendar.DAY_OF_WEEK);

		calendar.set(year, month, daysInMonth);
		int dayOfWeekLast = calendar.get(GregorianCalendar.DAY_OF_WEEK) - 1;

		if (dayOfWeekFirst == 8)
			dayOfWeekFirst = 1;
		for (int i = 0; i < 7 - dayOfWeekFirst; i++) {
			CalendarDayModel day = new CalendarDayModel();
			day.setTileType(CalendarTile.Invisible);
			days.add(day);
		}
		Date now = new Date();
		for (int i = 1; i <= daysInMonth; i++) {
			calendar.set(year, month, i);
			Date d = calendar.getTime();
			CalendarDayModel day = new CalendarDayModel();
			day.setLabel(String.valueOf(i));

			if (taskDates.contains(i)) {
				day.setTileType(CalendarTile.TaskDay);
			} else if (now.getYear() == d.getYear()
					&& now.getDate() == d.getDate()
					&& now.getMonth() == d.getMonth()) {
				day.setTileType(CalendarTile.CurrentDay);
			} else if (calendar.get(GregorianCalendar.DAY_OF_WEEK) == GregorianCalendar.SUNDAY
					|| calendar.get(GregorianCalendar.DAY_OF_WEEK) == GregorianCalendar.SATURDAY) {
				day.setTileType(CalendarTile.Holiday);
			} else {
				day.setTileType(CalendarTile.NormalDay);
			}
			day.setDate(calendar.getTime());
			days.add(day);
		}
		for (int i = 0; i < 7 - dayOfWeekLast; i++) {
			CalendarDayModel day = new CalendarDayModel();
			day.setTileType(CalendarTile.Invisible);
			days.add(day);
		}

		return new CalendarAdapter(ctx, R.layout.calendar_day, days);
	}

}
